﻿using IHateWorking.Model;
using IHateWorking.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;
using MenuItem = System.Windows.Controls.MenuItem;
using OfficeOpenXml;
using System.Data.SqlTypes;
//#JMJ Create_20240527

namespace IHateWorking.ViewModel
{
    /// <summary>
    /// 封面的 ViewModel
    /// </summary>
    public class VinylViewModel : INotifyPropertyChanged
    {
        public VinylViewModel()
        {
            Instance = this;
            ViewLoadedCommand = new CommandBase(ViewLoaded);
            BtnCommand = new CommandBase(BtnClick);
            KeyDownCommand = new CommandBase(KeyDown);
        }

        #region 变量的通知机制
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
        #endregion 以上：变量的通知机制

        #region //////////////////////////////静态变量//////////////////////////////
        /// <summary>
        /// 静态接口
        /// </summary>
        public static VinylViewModel Instance { get; set; }

        #endregion 以上：/////////////////////静态变量//////////////////////////////


        #region //////////////////////////////非静态变量////////////////////////////

        /// <summary>
        /// 界面加载完成 Command
        /// </summary>
        public CommandBase ViewLoadedCommand { get; set; }

        /// <summary>
        /// 按钮 Command
        /// </summary>
        public CommandBase BtnCommand { get; set; }

        /// <summary>
        /// 键盘按下 Command
        /// </summary>
        public CommandBase KeyDownCommand { get; set; }

        /// <summary>
        /// 窗体界面
        /// </summary>
        public Vinyl VinylView { get; set; }

        #endregion 以上：/////////////////////非静态变量////////////////////////////


        #region //////////////////////////////静态方法//////////////////////////////

        #endregion 以上：/////////////////////静态方法//////////////////////////////


        #region //////////////////////////////非静态方法////////////////////////////
        /// <summary>
        /// 界面加载完成事件
        /// </summary>
        /// <param name="view">窗体/用户控件</param>
        private void ViewLoaded(object view)
        {
            VinylView = view as Vinyl;

            VinylView.MouseDown += VinylView_MouseDown;
            VinylView.Closed += VinylView_Closed;

            #region 黑胶图案旋转
            Grid text = VinylView.grid_Text;

            //计算旋转中心
            Rect size = new Rect(text.RenderSize);
            double centerX = (size.TopLeft.X + size.TopRight.X + size.BottomLeft.X + size.BottomRight.X) / 4;
            double centerY = (size.TopLeft.Y + size.TopRight.Y + size.BottomLeft.Y + size.BottomRight.Y) / 4;

            text.RenderTransform = new RotateTransform(0, centerX, centerY);
            DoubleAnimation dbAscending = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(45)))
            { RepeatBehavior = RepeatBehavior.Forever, };
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(dbAscending);
            Storyboard.SetTarget(dbAscending, text);
            Storyboard.SetTargetProperty(dbAscending, new PropertyPath("RenderTransform.Angle"));
            storyboard.Begin();
            #endregion 以上：黑胶图案旋转
        }

        /// <summary>
        /// 按钮按下 Command 方法
        /// </summary>
        /// <param name="sender">触发的按钮</param>
        private void BtnClick(object sender)
        {
            string tag = "";
            if (sender is Button btn)
            { tag = btn.Tag == null ? "" : btn.Tag.ToString(); }

            switch (tag)
            {
                case "ShowAlbumIntr":
                    {
                        Music music = MusicListViewModel.Instance.OpenMusic;
                        if (IntroductionDic.Albums.ContainsKey(music.Album))
                        { MusicListViewModel.Instance.ShowIntrDetail(InfoType.专辑, music.Album, IntroductionDic.Albums[music.Album]); }
                        else
                        {
                            string infoStr = (music.Album == "" || music.Album.ToLower() == "null") ? "<" + music.Title + "/>未收录于专辑。" : "未找到<" + music.Album + "/>信息。";
                            MessageBox.Show(infoStr, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    break;
                case "ShowSingerIntr":
                    {
                        Music music = MusicListViewModel.Instance.OpenMusic;
                        if (IntroductionDic.Participants.ContainsKey(music.Singer))
                        { MusicListViewModel.Instance.ShowIntrDetail(InfoType.参与者, music.Singer, IntroductionDic.Participants[music.Singer]); }
                        else
                        {
                            string infoStr = music.Singer == "佚名" ? "<" + music.Title + "/>无演唱者信息。" : "未找到<" + music.Singer + "/>信息。";
                            MessageBox.Show(infoStr, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 键盘按下 Command 方法
        /// </summary>
        /// <param name="sender">触发的界面</param>
        private void KeyDown(object sender)
        {
            Vinyl view = sender as Vinyl;

            //Esc = 退出
            if (Keyboard.IsKeyDown(Key.Escape))
            {
                if (MusicListViewModel.Instance.Vis_Vinyl.StateMode > 0)
                {
                    MusicListViewModel.Instance.OpenMusic = null;
                    view.Close();
                }
            }
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VinylView_Closed(object sender, EventArgs e)
        { MusicListViewModel.Instance.Vis_Vinyl.StateMode = 0; }

        /// <summary>
        /// 主界面拖拽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VinylView_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            { VinylView.DragMove(); }
        }
        #endregion 以上：/////////////////////非静态方法////////////////////////////
    }
}
